﻿#ifndef NSSHARE_SYS_ASYNC_FILE_RESULT_H_
#define NSSHARE_SYS_ASYNC_FILE_RESULT_H_

#include "../utils/event_delegate.h"
#include "../utils/event_adapter.h"
#include "async_event_args.h"
#include "file_stream_define.h"


namespace nsshare
{
namespace sys
{

class FileStream;

/*文件异步操作状态基类*/
namespace AsyncFileStreamOp
{
enum Value
{
    kAsyncUnknow,
    kAsyncWrite,
    kAsyncRead,
    kAsyncLock,
    kAsyncUnlock,
};
}

class Event;
class FileStreamAsyncEventArgs;

typedef utils::EventDelegate<FileStreamAsyncEventArgs&> FileStreamAsyncEventDelegate;
typedef utils::EventHandler<FileStreamAsyncEventArgs&>  FileStreamAsyncEventHandler;

class FileStreamAsyncEventArgs : public AsyncEventArgs
{
public:
    FileStreamAsyncEventArgs();
    FileStreamAsyncEventArgs(Event & e);
    
    void SetBuffer(void * buffer, size_t count);
    void SetBuffer(const void * buffer, size_t count);

    void set_offset(uint32_t lo, uint32_t hi);
    void set_offset(uint64_t offset);
    void set_lock_mode(FileLockMode::Value mode);
    void set_lock_size(uint64_t size);
    void set_completion_delegate(FileStreamAsyncEventHandler * handler);

    void * data() const;
    size_t count() const;
    uint64_t offset() const;
    uint32_t error() const;
    uint32_t transfered() const;
    FileLockMode::Value lock_mode() const;
    uint64_t lock_size() const;
    AsyncFileStreamOp::Value last_op() const;

private:
    void OnCompleted(uint32_t error, uint32_t transfered);
private:
    void * data_;
    size_t count_;
    uint32_t error_;
    uint32_t transfered_;
    FileStreamAsyncEventDelegate completion_delegate_;
    FileLockMode::Value lock_mode_;
    uint64_t lock_size_;
    AsyncFileStreamOp::Value last_op_;

    friend class FileStreamRoutines;
    friend class FileStream;
};

template <typename Adaptee>
class FileStreamAsyncEventAdapter
    : public utils::EventAdapter<Adaptee, FileStreamAsyncEventArgs&>
{
};


}
}
#endif